package com.dxl.handler;

import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

/**
 * @author dxl
 * @date 2020/12/13 11:21
 */
@Component
public class MessageSourceHandler {
    @Resource
    private HttpServletRequest request;
    @Resource
    private MessageSource messageSource;

    public String getMessage(String messageKey) {
        Locale locale = RequestContextUtils.getLocale(request);
        return messageSource.getMessage(messageKey, null, getLocale(locale));
    }
    private Locale getLocale(Locale locale) {
        if (locale.equals(Locale.ENGLISH) || locale.equals(Locale.US) || locale.equals(Locale.UK)) {
            return Locale.ENGLISH;
        }

        return Locale.CHINESE;
    }
}